package com.duckclouddrive.controller;

import java.util.List;

import com.duckclouddrive.annotation.GlobalInterceptor;
import com.duckclouddrive.annotation.VerifyParam;
import com.duckclouddrive.entity.dto.SessionWebUserDto;
import com.duckclouddrive.entity.query.FileShareQuery;
import com.duckclouddrive.entity.po.FileShare;
import com.duckclouddrive.entity.vo.PaginationResultVO;
import com.duckclouddrive.entity.vo.ResponseVO;
import com.duckclouddrive.service.FileShareService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 分享信息 Controller
 */
@RestController("fileShareController")
@RequestMapping("/share")
public class ShareController extends ABaseController{

	@Resource
	private FileShareService fileShareService;

	@RequestMapping("/loadShareList")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO loadShareList(HttpSession session, FileShareQuery query) {
		query.setOrderBy("share_time desc");
		SessionWebUserDto userDto = getUserInfoFromSession(session);
		query.setUserId(userDto.getUserId());
		query.setQueryFileName(true);
		PaginationResultVO resultVO = this.fileShareService.findListByPage(query);
		return getSuccessResponseVO(resultVO);
	}

	@RequestMapping("/shareFile")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO shareFile(    HttpSession session,
			                        @VerifyParam(required = true) String fileId,
									@VerifyParam(required = true) Integer validType,
								    String code) {
         SessionWebUserDto userDto = getUserInfoFromSession(session);
         FileShare share = new  FileShare();
		 share.setCode(code);
		 share.setValidType(validType);
		 share.setUserId(userDto.getUserId());
		 share.setFileId(fileId);
		 fileShareService.saveShare(share);
		 return getSuccessResponseVO(share);
	}
	@RequestMapping("/cancelShare")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO cancelShare(HttpSession session,
								  @VerifyParam(required = true) String shareIds,
								  @VerifyParam(required = true) Integer validType,
								  String code) {
		SessionWebUserDto userDto = getUserInfoFromSession(session);
		fileShareService.deleteShareBatch(shareIds.split(","),userDto.getUserId());
		return getSuccessResponseVO(null);
	}
	/**
	 * 根据条件分页查询
	 */
	@RequestMapping("/loadDataList")
	public ResponseVO loadDataList(FileShareQuery query){
		return getSuccessResponseVO(fileShareService.findListByPage(query));
	}

	/**
	 * 新增
	 */
	@RequestMapping("/add")
	public ResponseVO add(FileShare bean) {
		fileShareService.add(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增
	 */
	@RequestMapping("/addBatch")
	public ResponseVO addBatch(@RequestBody List<FileShare> listBean) {
		fileShareService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增/修改
	 */
	@RequestMapping("/addOrUpdateBatch")
	public ResponseVO addOrUpdateBatch(@RequestBody List<FileShare> listBean) {
		fileShareService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据ShareId查询对象
	 */
	@RequestMapping("/getFileShareByShareId")
	public ResponseVO getFileShareByShareId(String shareId) {
		return getSuccessResponseVO(fileShareService.getFileShareByShareId(shareId));
	}

	/**
	 * 根据ShareId修改对象
	 */
	@RequestMapping("/updateFileShareByShareId")
	public ResponseVO updateFileShareByShareId(FileShare bean,String shareId) {
		fileShareService.updateFileShareByShareId(bean,shareId);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据ShareId删除
	 */
	@RequestMapping("/deleteFileShareByShareId")
	public ResponseVO deleteFileShareByShareId(String shareId) {
		fileShareService.deleteFileShareByShareId(shareId);
		return getSuccessResponseVO(null);
	}
}